//将测试需求审核的表单除编辑器以外的内容清空
function testRequrementCheckFormReset() {
    testRequirementCheckForm.down("#testRequirementCheckFormName").setValue();
    testRequirementCheckForm.down("#testRequirementCheckFormIsPass").setValue();
    testRequirementCheckForm.down("#testRequirementCheckFormCheckDate").setValue();
    testRequirementCheckForm.down("#testRequirementCheckFormAnnotate").setValue();
    testRequirementCheckForm.down("#testRequirementCheckFormDescribe").setValue();
    testRequirementCheckForm.down('#testRequirementCheckFormSaveAndCommitBtn').setDisabled(false);
}

//将测试需求审核的详细信息填充到查看表单
function testRequrementCheckDataToForm(record) {
    var data = record.data;
    var status = testCheckGrid.down('#testCheckGridStatusCombo').lastValue;
    var tId = status != -1 ? data.id : data.targetId;//如果是待审核的话，传过来的id就是指对应的测试需求Id
    Ext.Ajax.request({//然后去后台请求该审核的批注
        url: zuulUrl + '/' + testServiceName + '/testRequirementCheck/detail/' + status + '/' + tId,
        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (status != -1)
                testRequirementCheckForm.testCheckId = data.id;//将审核Id保存查看审核表单
            testRequirementCheckForm.down("#testRequirementCheckFormName").setValue(obj.targetName);
            testRequirementCheckForm.down("#testRequirementCheckFormIsPass").setValue(obj.result == 1 ? {isPass: 1} : {isPass: 0});
            testRequirementCheckForm.down("#testRequirementCheckFormCheckDate").setValue(obj.checkDate == null ? '无' : obj.checkDate.replace('T', '  '));
            testRequirementCheckForm.down("#testRequirementCheckFormAnnotate").setValue(obj.annotate);
            testRequirementCheckForm.down("#testRequirementCheckFormDescribe").setValue(obj.targetDescribe);
            testRequirementCheckForm.targetId = obj.targetId;//将审核对象的Id储存到测试审核的表单中
            if (obj.isDeleted == 1)//如果审核对象已经被删除了的话
                testRequirementCheckForm.down('#testRequirementCheckFormSaveAndCommitBtn').setDisabled(true);
        },
        failure: function (response, opts) {
            Ext.Msg.alert('失败', '该操作暂时不可用！');
        }
    });
}

//将测试计划审核的详情信息的TabPanel下的所有信息都清空
function testDesignCheckFormReset() {
    //********************************基本信息填充begin****************************************//
    testDesignDetailForm.down('#testDesignDetailFormTitle').setValue();
    testDesignDetailForm.down('#testDesignDetailFormAuthor').setValue();
    testDesignDetailForm.down('#testDesignDetailFormPriority').setValue();
    testDesignDetailForm.down('#testDesignDetailFormDescription').setValue();
    testDesignDetailForm.down('#testDesignDetailFormCreateDate').setValue();
    testDesignDetailForm.down('#testDesignDetailFormUpdateDate').setValue();
    //********************************基本信息填充end****************************************//
    //********************************测试步骤填充begin****************************************//
    testDesignStepStore.removeAll();
    //********************************测试步骤填充end****************************************//
    //********************************关联需求填充begin****************************************//
    testDesignTestRequirmentStore.removeAll();
    //********************************关联需求填充end****************************************//
    testDesignCheckResultForm.down('#testCheckFormIsPass').setValue();
    testDesignCheckResultForm.down('#testCheckFormCheckDate').setValue();
    testDesignCheckResultForm.down('#testCheckFormAnnotate').setValue();
    testDesignCheckTabPanel.down('#testDesignCheckSaveAndCommitButton').setDisabled(false);
    testDesignCheckTabPanel.down('#testDesignCheckSaveButton').setDisabled(false);
    testDesignCheckTabPanel.setActiveTab(0);
}

//将测试计划审核的详情信息的TabPanel下的所有信息都加载
function testDesignCheckDataToForm(record) {
    var data = record.data;
    var status = testCheckGrid.down('#testCheckGridStatusCombo').lastValue;
    var tId = status != -1 ? data.id : data.targetId;//如果是待审核的话，传过来的id就是指对应的测试计划Id
    Ext.Ajax.request({//然后去后台请求该审核的批注
        url: zuulUrl + '/' + testServiceName + '/testDesignCheck/detail/' + status + '/' + tId,
        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.isDeleted == 1) {//如果审核对象已经被删除了的话
                testDesignCheckTabPanel.down('#testDesignCheckSaveAndCommitButton').setDisabled(true);
                testDesignCheckTabPanel.down('#testDesignCheckSaveButton').setDisabled(true);
                Ext.Msg.alert('错误', '审核对象已被删除！');
            } else {
                //********************************基本信息填充begin****************************************//
                var targetDescribe = obj.targetDescribe;
                testDesignCheckDetailForm.down("#testDesignDetailFormTitle").setValue(obj.targetName);
                testDesignCheckDetailForm.down("#testDesignDetailFormAuthor").setValue(targetDescribe.author);
                testDesignCheckDetailForm.down("#testDesignDetailFormPriority").setValue(targetDescribe.priority);
                testDesignCheckDetailForm.down('#testDesignDetailFormDescription').setValue(targetDescribe.describe);
                testDesignCheckDetailForm.down('#testDesignDetailFormCreateDate').setValue(targetDescribe.createDate.replace('T', '    '));
                testDesignCheckDetailForm.down('#testDesignDetailFormUpdateDate').setValue(targetDescribe.updateDate.replace('T', '    '));
                //********************************基本信息填充end****************************************//
                //********************************测试步骤填充begin****************************************//
                testDesignStepStore.removeAll();//setData之前要先removeAll,不然会堆栈溢出，原因不明
                var afterShotStep = testDesignBorderShot(targetDescribe.step);
                testDesignStepStore.setData(afterShotStep);
                //********************************测试步骤填充end****************************************//
                //********************************关联需求填充begin****************************************//
                testDesignTestRequirmentStore.removeAll();//setData之前要先removeAll,不然会堆栈溢出，原因不明
                testDesignTestRequirmentStore.setData(targetDescribe.testReq);
                //********************************关联需求填充end****************************************//
                //********************************审核结果表单填充begin****************************************//
                if (status != -1)
                    testDesignCheckResultForm.testCheckId = data.id;//将审核Id保存查看审核表单.
                testDesignCheckResultForm.down("#testCheckFormIsPass").down('#yes').setValue(obj.result == 1);
                testDesignCheckResultForm.down("#testCheckFormIsPass").down('#no').setValue(obj.result == 0);
                testDesignCheckResultForm.down("#testCheckFormCheckDate").setValue(obj.checkDate == null ? '无' : obj.checkDate.replace('T', '  '));
                testDesignCheckResultForm.down("#testCheckFormAnnotate").setValue(obj.annotate);
                testDesignCheckResultForm.targetId = obj.targetId;//将审核对象的Id储存到测试审核的表单中
                //********************************审核结果表单填充end****************************************//
            }
        },
        failure: function (response, opts) {
            Ext.Msg.alert('失败', '该操作暂时不可用！');
        }
    });
}

//将测试执行审核的详情信息的TabPanel下的所有信息都清空
function testRunCheckFormReset() {
    outSideSteps = [];//清空数组
    //********************************基本信息填充begin****************************************//
    testRunCheckDetailForm.down('#testRunDetailFormTitle').setValue();
    testRunCheckDetailForm.down('#testRunDetailFormAuthor').setValue();
    testRunCheckDetailForm.down('#testRunDetailFormPriority').setValue();
    testRunCheckDetailForm.down('#testRunDetailFormTestDesign').setValue();
    testRunCheckDetailForm.down('#testRunDetailFormDescription').setValue();
    testRunCheckDetailForm.down('#testRunDetailFormCreateDate').setValue();
    testRunCheckDetailForm.down('#testRunDetailFormUpdateDate').setValue();
    //********************************基本信息填充end****************************************//
    //********************************执行记录填充begin****************************************//
    testRunlogRowWidGetStore.removeAll();
    testRunLogStepRowWidGetStore.removeAll();
    //********************************执行记录填充end****************************************//
    testRunCheckResultForm.down('#testCheckFormIsPass').setValue();
    testRunCheckResultForm.down('#testCheckFormCheckDate').setValue();
    testRunCheckResultForm.down('#testCheckFormAnnotate').setValue();
    testRunCheckTabPanel.down('#testRunCheckSaveAndCommitButton').setDisabled(false);
    testRunCheckTabPanel.down('#testRunCheckSaveButton').setDisabled(false);
}

//将测试执行审核的详情信息的TabPanel下的所有信息都加载
function testRunCheckDataToForm(record) {
    var data = record.data;
    var status = testCheckGrid.down('#testCheckGridStatusCombo').getValue();
    var tId = status != -1 ? data.id : data.targetId;//如果是待审核的话，传过来的id就是指对应的测试计划Id
    Ext.Ajax.request({//然后去后台请求该审核的批注
        url: zuulUrl + '/' + testServiceName + '/testRunCheck/detail/' + status + '/' + tId,
        async: true,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.isDeleted == 1) {//如果审核对象已经被删除了的话
                testRunCheckTabPanel.down('#testRunCheckSaveAndCommitButton').setDisabled(true);
                testRunCheckTabPanel.down('#testRunCheckSaveButton').setDisabled(true);
                Ext.Msg.alert('错误', '审核对象已被删除！');
            } else {
                //********************************基本信息填充begin****************************************//
                var targetDescribe = obj.targetDescribe;
                testRunCheckDetailForm.down("#testRunDetailFormTitle").setValue(obj.targetName);
                testRunCheckDetailForm.down("#testRunDetailFormAuthor").setValue(targetDescribe.author);
                testRunCheckDetailForm.down("#testRunDetailFormPriority").setValue(targetDescribe.priority);
                testRunCheckDetailForm.down('#testRunDetailFormTestDesign').setValue(targetDescribe.testDesignName);
                testRunCheckDetailForm.down('#testRunDetailFormDescription').setValue(targetDescribe.describe);
                testRunCheckDetailForm.down('#testRunDetailFormCreateDate').setValue(targetDescribe.createDate.replace('T', '    '));
                testRunCheckDetailForm.down('#testRunDetailFormUpdateDate').setValue(targetDescribe.updateDate.replace('T', '    '));
                //********************************基本信息填充end****************************************//
                //********************************测试步骤填充begin****************************************//
                //将执行记录结构体的内容取出来放到外层
                for (var i = 0, length = targetDescribe.testRunLogDto.length; i < length; i++) {
                    //将执行记录结构体的内容取出来放到外层
                    targetDescribe.testRunLogDto[i].id = targetDescribe.testRunLogDto[i].testRunLog.id;
                    targetDescribe.testRunLogDto[i].testRunId = targetDescribe.testRunLogDto[i].testRunLog.testRunId;
                    targetDescribe.testRunLogDto[i].host = targetDescribe.testRunLogDto[i].testRunLog.host;
                    targetDescribe.testRunLogDto[i].status = targetDescribe.testRunLogDto[i].testRunLog.status;
                    targetDescribe.testRunLogDto[i].duration = targetDescribe.testRunLogDto[i].testRunLog.duration;
                    targetDescribe.testRunLogDto[i].browserVersion = targetDescribe.testRunLogDto[i].testRunLog.browserVersion;
                    targetDescribe.testRunLogDto[i].os = targetDescribe.testRunLogDto[i].testRunLog.os;
                    targetDescribe.testRunLogDto[i].createDate = targetDescribe.testRunLogDto[i].testRunLog.createDate;
                    targetDescribe.testRunLogDto[i].testRunLog = null;
                    //将每个测试执行记录下的执行步骤进行排序
                    targetDescribe.testRunLogDto[i].testRunLogStep = testDesignBorderShot(targetDescribe.testRunLogDto[i].testRunLogStep);
                }
                testRunLogStepRowWidGetStore.removeAll();
                testRunlogRowWidGetStore.removeAll();
                testRunlogRowWidGetStore.setData(targetDescribe.testRunLogDto);// 不知为何使用add方式一个个加会堆栈溢出，只能使用setData()方式
                //********************************测试步骤填充end****************************************//
                //********************************审核结果表单填充begin****************************************//
                if (status != -1)
                    testRunCheckResultForm.testCheckId = data.id;//将审核Id保存查看审核表单.
                testRunCheckResultForm.down("#testCheckFormIsPass").down('#yes').setValue(obj.result == 1);
                testRunCheckResultForm.down("#testCheckFormIsPass").down('#no').setValue(obj.result == 0);
                testRunCheckResultForm.down("#testCheckFormCheckDate").setValue(obj.checkDate == null ? '无' : obj.checkDate.replace('T', '  '));
                testRunCheckResultForm.down("#testCheckFormAnnotate").setValue(obj.annotate);
                testRunCheckResultForm.targetId = obj.targetId;//将审核对象的Id储存到测试审核的表单中
                //********************************审核结果表单填充end****************************************//
            }
        },
        failure: function (response, opts) {
            Ext.Msg.alert('失败', '该操作暂时不可用！');
        }
    });
}

//根据对象类型和审核状态下拉框的值去获取对应的审核
function getTestCheckByCombo(typeValue, statusValue) {
    if (statusValue == -1) {//如果选择的状态是待审核的话就去把审核结果和审核时间的列给隐藏
        testCheckGrid.columns[2].hide();
        testCheckGrid.columns[3].hide();
    } else {
        testCheckGrid.columns[2].show();
        testCheckGrid.columns[3].show();
        testCheckGrid.columns[5].hide();//只有待审核的页面才有授权操作
    }
    if (typeValue == 0) {//说明要查看测试需求类型的审核
        testCheckGrid.columns[1].hide();//测试设计才有显示所属主题文件夹
        testCheckGridStore.proxy.url = zuulUrl + '/' + testServiceName + '/testRequirementCheck/checkMan/' + currentProjectId + '/' + currentUserId + '/' + statusValue;
    } else if (typeValue == 1) {//说明是查看测试设计类型的审核
        testCheckGrid.columns[1].show();//测试设计和测试执行才有显示所属主题文件夹
        testCheckGridStore.proxy.url = zuulUrl + '/' + testServiceName + '/testDesignCheck/checkMan/' + currentProjectId + '/' + currentUserId + '/' + statusValue;
    } else if (typeValue == 2) {//说明是去查看测试执行类型的审核
        testCheckGrid.columns[1].show();//测试设计和测试执行才有显示所属主题文件夹
        testCheckGridStore.proxy.url = zuulUrl + '/' + testServiceName + '/testRunCheck/checkMan/' + currentProjectId + '/' + currentUserId + '/' + statusValue;
    }
    testCheckGridStore.reload();//刷新store
}

//保存审核结果和审核批注
function saveTestCheckAnnnotateAndIsPass(isFinish) {
    var comboType = testCheckGrid.down('#testCheckGridTypeCombo').getValue();
    var comboStatus = testCheckGrid.down('#testCheckGridStatusCombo').getValue();
    var annotate, isPass, targetId, testCheckId;
    if (comboType == 0) {
        annotate = testRequirementCheckForm.down("#testRequirementCheckFormAnnotate").getValue();
        isPass = testRequirementCheckForm.down("#testRequirementCheckFormIsPass").lastValue.isPass;//checkBox要这样子才能拿到值
        targetId = testRequirementCheckForm.targetId;
        testCheckId = testRequirementCheckForm.testCheckId;
    } else if (comboType == 1) {
        annotate = testDesignCheckResultForm.down("#testCheckFormAnnotate").getValue();
        isPass = testDesignCheckResultForm.down("#testCheckFormIsPass").lastValue.isPass;//checkBox要这样子才能拿到值
        targetId = testDesignCheckResultForm.targetId;
        testCheckId = testDesignCheckResultForm.testCheckId;
    } else if (comboType == 2) {
        annotate = testRunCheckResultForm.down("#testCheckFormAnnotate").getValue();
        isPass = testRunCheckResultForm.down("#testCheckFormIsPass").lastValue.isPass;//checkBox要这样子才能拿到值
        targetId = testRunCheckResultForm.targetId;
        testCheckId = testRunCheckResultForm.testCheckId;
    }
    var resultArray = new Array();
    var object = new Object();
    if (comboStatus != -1)//如果不是待审核界面的话,就只要给审核Id就好了
        object['id'] = testCheckId;
    else//不然就直接给targetId就好了
        object['targetId'] = targetId;
    object['checkMan'] = currentUserId;
    object['annotate'] = annotate;
    object['isPass'] = isPass;
    if (comboType == 1 || comboType == 2)//如果是测试计划和城市执行就要将isFinish附上
        object['isFinish'] = isFinish;
    resultArray.push(object);
    var url = zuulUrl + '/' + testServiceName;
    if (comboType == 0) {//如果是测试需求审核的话
        url = url + '/testRequirementCheck';
        if (comboStatus == 0)//如果是在审核中的页面的话
            url = url + '/annoAndIsPass/0';//后面的0代表现在处于审核中的界面
        else if (comboStatus == 1)//如果是在审核完成的页面的话
            url = url + '/annoAndIsPass/1';//后面的0代表现在处于审核完成的界面
    } else if (comboType == 1) {//如果是测试计划审核的话
        url = url + '/testDesignCheck';
        if (comboStatus == 0 || comboStatus == 1)//如果是在审核中或者审核完成的页面的话
            url = url + '/annoAndIsPass';//后面的0代表现在处于审核中的界面
    } else {//如果是测试执行审核的话
        url = url + '/testRunCheck';
        if (comboStatus == 0 || comboStatus == 1)//如果是在审核中或者审核完成的页面的话
            url = url + '/annoAndIsPass';//后面的0代表现在处于审核中的界面
    }
    Ext.Ajax.request({
        url: url,
        method: comboStatus == -1 ? 'post' : 'put',
        async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        defaultXdrContentType: 'application/json',
        jsonData: JSON.stringify(resultArray),
        success: function (response, opts) {
            Ext.Msg.alert("Success", "保存成功");
            if (comboStatus == -1) {
                if (comboType == 0)//如果是测试需求审核的话
                    testRequirementCheckForm.down('#testRequirementCheckFormSaveAndCommitBtn').setDisabled(true);
                else if (comboType == 1) {//如果是测试计划审核的话
                    testDesignCheckTabPanel.down('#testDesignCheckSaveAndCommitButton').setDisabled(true);
                    testDesignCheckTabPanel.down('#testDesignCheckSaveButton').setDisabled(true);
                } else {//如果是测试执行审核的话
                    testRunCheckTabPanel.down('#testRunCheckSaveAndCommitButton').setDisabled(true);
                    testRunCheckTabPanel.down('#testRunCheckSaveButton').setDisabled(true);
                }
            }
            testCheckGridStore.reload();
        },
        failure: function (response, opts) {
            Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
}

//递归树形菜单并且全部展开
function recurrenceSpread(id) {
    var record = testCheckGridStore.findRecord('id', id);
    if (!record.get('leaf')) {
        if (!record.isExpanded() && record.get('isHas') == 1)
            record.expand();//插入子节点前先设置为展开,不然没有效果
        if (record.get('isHas') == 0) {
            record.set('expanded', true);// 插入子节点前先设置为展开,不然没有效果
            var status = testCheckGrid.down('#testCheckGridStatusCombo').getValue();
            var id = status == -1 ? record.get('targetId') : record.get('id');
            Ext.Ajax.request({
                url: zuulUrl + '/' + testServiceName + '/testRequirementCheck/children/' + status + '/' + id,
                method: 'get',
                async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                defaultXdrContentType: 'application/json',
                success: function (response, opts) {
                    var obj = Ext.decode(response.responseText);
                    if (obj.code == 200) {
                        record.appendChild(obj.data);
                        record.set("isHas", 1);
                    } else
                        Ext.Msg.alert("错误", "该功能暂时不可用！");
                },
                failure: function (response, opts) {
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
                }
            });
        }
        var children = testCheckGridStore.queryRecords('parentId', record.get('id'));
        Ext.each(children, function (child) {
            recurrenceSpread(child.get('id'));
        });
    }
}

//给出审核结果
function judgeCheck(isPass) {
    var comboStatus = testCheckGrid.down('#testCheckGridStatusCombo').getValue();
    var comboType = testCheckGrid.down('#testCheckGridTypeCombo').getValue();
    var selectArray = testCheckGrid.getSelectionModel().getSelected(); // 得到被选择的行的数组.
    var resultArray = new Array();
    Ext.each(selectArray.items, function (record) {
        var object = new Object();
        if (comboStatus != -1)//如果不是待审核界面的话
            object['id'] = record.data.id;
        else//如果是待审核界面就只是传targetId就好了
            object['targetId'] = record.data.targetId;
        object['checkMan'] = currentUserId;
        object['annotate'] = ' ';
        object['isPass'] = isPass;
        if (comboType == 1 || comboType == 2)
            object['isFinish'] = 1;
        resultArray.push(object);
    })
    var url = zuulUrl + '/' + testServiceName;
    if (comboType == 0) {//如果是测试需求审核的话
        url = url + '/testRequirementCheck';
        if (comboStatus == 0)//如果是在审核中的页面的话
            url = url + '/annoAndIsPass/0';//后面的0代表现在处于审核中的界面
        else if (comboStatus == 1)//如果是在审核完成的页面的话
            url = url + '/annoAndIsPass/1';//后面的0代表现在处于审核完成的界面
    } else if (comboType == 1) {//如果是测试计划审核的话
        url = url + '/testDesignCheck';
        if (comboStatus == 0 || comboStatus == 1)//如果是在审核中或者审核完成的页面的话
            url = url + '/annoAndIsPass';
    } else {//如果是测试执行审核的话
        url = url + '/testRunCheck';
        if (comboStatus == 0 || comboStatus == 1)//如果是在审核中或者审核完成的页面的话
            url = url + '/annoAndIsPass';
    }
    Ext.Ajax.request({
        url: url,
        method: comboStatus == -1 ? 'post' : 'put',
        async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        defaultXdrContentType: 'application/json',
        jsonData: JSON.stringify(resultArray),
        success: function (response, opts) {
            testCheckGridStore.reload();
        },
        failure: function (response, opts) {
            Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
}

//点击下拉按钮后的操作
function expendItem(record) {
    if (record.get('id') != 'root') {
        if (!record.get('leaf') && record.get('isHas') == 0) {
            var status = testCheckGrid.down('#testCheckGridStatusCombo').getValue();
            var id = status == -1 ? record.get('targetId') : record.get('id');
            Ext.Ajax.request({
                url: zuulUrl + '/' + testServiceName + '/testRequirementCheck/children/' + status + '/' + id,
                method: 'get',
                async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                success: function (response, opts) {
                    var obj = Ext.decode(response.responseText);
                    if (obj.code == 200) {
                        record.appendChild(obj.data);
                        record.set("isHas", 1);
                    } else
                        Ext.Msg.alert("错误", "该功能暂时不可用！");
                },
                failure: function (response, opts) {
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
                }
            });
        }
    }
}